package studychinese.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hschinese.pronunciation.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.adapters.MySeleteBasicAdapter;
import studychinese.fragment.NativeBirthdayFragment;
import studychinese.fragment.NativeCityFragment;
import studychinese.fragment.NativeLanguageFragment;
import studychinese.fragment.NativeStudayPlanFragment;
import studychinese.fragment.WhyStudayCZFragment;

public class MySeleteBasicActivity extends BaseAcitivity implements ViewPager.OnPageChangeListener{


    @Bind(R.id.guide_vp)
    ViewPager guideVp;
    @Bind(R.id.ll_indication)
    LinearLayout llIndication;
     private List<Fragment> list;
    public ImageView[] dotViews;
    @Override
    protected int setContainer() {
        return R.layout.activity_my_selete_basic;
    }

    @Override
    public void innitView() {
        ButterKnife.bind(this);
        //把Fragment添加到List集合里面
        list = new ArrayList<>();
            list.clear();
            list.add(NativeLanguageFragment.newInstance(""));
            list.add(NativeCityFragment.newInstance(""));
            list.add(NativeBirthdayFragment.newInstance(""));
            list.add(WhyStudayCZFragment.newInstance());
            list.add(NativeStudayPlanFragment.newInstance());

        MySeleteBasicAdapter mySeleteBasicAdapter = new MySeleteBasicAdapter(getSupportFragmentManager(), list);
        guideVp.setOffscreenPageLimit(4);
        guideVp.setAdapter(mySeleteBasicAdapter);
        initImgs();
        guideVp.setOnPageChangeListener(this);

    }
    private void initImgs() {

        initDots();
    }
    private void initDots()
    {

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mParams.setMargins(10, 0, 10, 0);//设置小圆点左右之间的间隔
        dotViews = new ImageView[list.size()];
        for(int i = 0; i < list.size(); i++)
        {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(mParams);
            imageView.setImageResource(R.drawable.mybasicshape);
            if(i== 0)
            {
                imageView.setSelected(true);//默认启动时，选中第一个小圆点
            }
            else {
                imageView.setSelected(false);
            }
            dotViews[i] = imageView;//得到每个小圆点的引用，用于滑动页面时，（onPageSelected方法中）更改它们的状态。
            llIndication.addView(imageView);//添加到布局里面显示
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for(int i = 0; i < dotViews.length; i++)
        {
            if(position == i)
            {
                dotViews[i].setSelected(true);
            }
            else {
                dotViews[i].setSelected(false);
            }
        }


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 设置一个ViewPager的侦听事件，当左右滑动ViewPager时菜单栏被选中状态跟着改变
     *
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:

                    break;
                case 1:

                    break;
                case 2:

                    break;
            }
        }
    }
}
